// var storage = localStorage.getItem('detail')
// // console.log(storage);
// storage = JSON.parse(storage)

// $('.goods_item ul li img').attr('src', storage.hd_thumb_url)

// $('.price-m').html(storage.market_price / 100)

// $('.price-p').html(storage.normal_price / 100)



// 1. 渲染页面 :
// 先去获取一下localStorage ;
var cart_list = (function () {
    var storage = localStorage.getItem("carts");
    if (storage === null) {
        return [];
    }
    return JSON.parse(storage);
})();

function render() {
    var html = "";
    var total_price = 0;
    for (var i = 0; i < cart_list.length; i++) {

        var price = (cart_list[i].normal_price / 100).toFixed(2);
        var count = cart_list[i].count
        var now_price = price * count;
        total_price += now_price;
        html += `<ul class="product-item"  data-index="${i}">
                    <li class="col-md-3">
                        <img src="${cart_list[i].hd_thumb_url}" alt="">
                        <a href="">${cart_list[i].short_name}</a>
                    </li>
                    <li class="col-md-2 shuliang">
                        <button class='jian'>-</button>
                        <input type="text" value="${count}" class="counts">   
                        <button class='jia'>+</button>
                    </li>
                    <li class="col-md-2">

                    </li>
                    <li class="col-md-1 price-m">
                        ${(cart_list[i].market_price/100).toFixed(2)}
                    </li>
                    <li class="col-md-1 price-p">
                    ${(cart_list[i].normal_price/100).toFixed(2)}
                    </li>
                    <li class="col-md-2">
                        ${now_price}
                    </li>
                    <li class="col-md-1 del">
                        <a href="">删除</a>
                    </li> 
                </ul> `
    }

    $(".goods_item").html(html);
    $(".zongji i").html(total_price);
}

render()

$(".goods_item").on("click", "button:contains(-)", function () {
    var index = $(this).parents(".product-item").attr("data-index");
    cart_list[index].count--;
    if (cart_list[index].count <= 1) {
        cart_list[index].count = 1
    }


    render();
    localStorage.setItem("carts", JSON.stringify(cart_list));
})


$(".goods_item").on("click", "button:contains(+)", function () {
    var index = $(this).parents(".product-item").attr("data-index");
    cart_list[index].count++;

    render();
    localStorage.setItem("carts", JSON.stringify(cart_list));
})


$(".goods_item").on("click", ".del a", function () {
    var index = $(this).parents(".product-item").attr("data-index");
    cart_list.splice(index, 1)
    render();
    localStorage.setItem("carts", JSON.stringify(cart_list));
})

$('.clear').click(function () {
    cart_list = []
    render()
    localStorage.setItem('carts', JSON.stringify(cart_list))
    location.assign('../cart_kong.html')
})

$('.continue').click(function () {

    location.assign('../goods-list.html')

})